package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpCampScoreRecord;
import com.yanqu.road.entity.activity.xiuxian.data.CrossXiuXianPvpData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXiuXianPvpDataDaoImpl extends TempDataObjectDao<CrossXiuXianPvpData> {

    public static String getCrossXiuXianPvpDataSql = "select * from `t_c_xiuxian_pvp_data` where `activityId`=?";

    public CrossXiuXianPvpData getCrossXiuXianPvpData(int activityId) {
        CrossXiuXianPvpData crossXiuXianPvpData = null;
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossXiuXianPvpDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                crossXiuXianPvpData = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return crossXiuXianPvpData;
    }

    public static String insertCrossXiuXianPvpDataSql = "insert into `t_c_xiuxian_pvp_data`(activityId,lockState,genCampState,round,activityState,campScoreRecordMap,updateTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossXiuXianPvpData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockState()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGenCampState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampScoreRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertCrossXiuXianPvpDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossXiuXianPvpDataSql = "update `t_c_xiuxian_pvp_data` set lockState=?,genCampState=?,round=?,activityState=?,campScoreRecordMap=?,updateTime=? where activityId=? ";

    @Override
    protected boolean updateData(CrossXiuXianPvpData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isLockState()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGenCampState()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCampScoreRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateCrossXiuXianPvpDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossXiuXianPvpData parseData(ResultSet rs) throws SQLException {
        CrossXiuXianPvpData data = new CrossXiuXianPvpData();
        data.setActivityId(rs.getInt("activityId"));
        data.setLockState(rs.getBoolean("lockState"));
        data.setGenCampState(rs.getBoolean("genCampState"));
        data.setRound(rs.getInt("round"));
        data.setActivityState(rs.getInt("activityState"));
        data.setCampScoreRecordMap(JSON.parseObject(rs.getString("campScoreRecordMap"), new TypeReference<ConcurrentHashMap<Integer, Map<Integer, CrossXiuXianPvpCampScoreRecord>>>() {
        }));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }
}
